package algorithm.minitree;

import java.util.Vector;

public class DenseWeightedGraph<Weight extends Number & Comparable> implements  WeightedGraph {

    private int n;

    private int m;

    private boolean directed;

    private  Edge<Weight>[][] g;

    public DenseWeightedGraph(int n, boolean directed) {
        this.n = n;
        m = 0;
        this.directed = directed;
        g = new Edge[n][n];
        for(int i=0;i<n;i++){
            for(int  j=0;j<n;j++){
                g[i][j] = null;
            }
        }
    }

    @Override
    public int V() {
        return n;
    }

    @Override
    public int E() {
        return m;
    }

    @Override
    public void addEdge(Edge e) {
        if(hasEdge(e.a(),e.b())){
            return;
        }
        g[e.a()][e.b()]=new Edge<>(e);
        if(e.a()!=e.b()&&!directed){
            g[e.b()][e.a()]=new Edge<>(e);
        }
        m++;
        }

    @Override
    public boolean hasEdge(int v, int w) {

        return g[v][w]==null?false:true;
    }

    @Override
    public void show() {
     for(int i = 0;i < n;i++){
         for(int j = 0;j < n;j++){
            if(g[i][j] != null){
                System.out.print(g[i][j].wt()+"\t");
             }
             else{
                System.out.print("NULL\t");
            }

         }
         System.out.println();
     }

    }

    @Override
    public Iterable<Edge> adj(int v) {

        Vector<Edge> vector = new Vector();

         for(int i=0;i<n;i++){
             if(g[v][i]!=null){
               vector.add(g[v][i]);
             }
         }


        return vector;
    }
}
